package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
@Autowired
    private OrderSettingService orderSettingService;
/*
* 查询预约
* */
@GetMapping("/getOrderSettingByMonth")
public Result SelectSetting(Integer year, Integer month){
   List<OrderSettingParam> orderSettingss =  orderSettingService.listOrder(year,month);
   return new Result(true, MessageConst.QUERY_ORDER_SUCCESS,orderSettingss);
}
/*
* 添加预约人数
* */
    @PostMapping("/editNumberByDate")
    public Result add(@RequestBody OrderSetting orderSetting){
        orderSettingService.add(orderSetting);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
    /*
    * 批量添加
    * */
    @PostMapping("/upload")
    public Result listadd(MultipartFile excelFile){
        if(excelFile == null){
            throw new BusinessRuntimeException(MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
        //获取文件名
        String originalFilename = excelFile.getOriginalFilename();
        //获取文件后缀
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!substring.endsWith(".xlsx") || substring.endsWith(".xls")){
            throw new BusinessRuntimeException("请输入文件格式为xlsx或者是xls的文件");
        }
        orderSettingService.upload(excelFile);
        return new Result(true,MessageConst.UPLOAD_SUCCESS);
    }
}
